package uk.ac.nott.cs.g53dia.bee;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import uk.ac.nott.cs.g53dia.Action;
import uk.ac.nott.cs.g53dia.ActionFailedException;
import uk.ac.nott.cs.g53dia.Bee;
import uk.ac.nott.cs.g53dia.BeeViewer;
import uk.ac.nott.cs.g53dia.Cell;
import uk.ac.nott.cs.g53dia.DeadBeeException;
import uk.ac.nott.cs.g53dia.Environment;

/**
 * An example of how to simulate execution of a bee agent in the sample (task) environment.
 * <p>
 * Creates a default {@link Environment}, a {@link DemoBee} and a GUI window 
 * (a {@link BeeViewer}) and executes the bee for DURATION days in the environment. 
 * 
 * @author Julian Zappala
 */

/*
 * Copyright (c) 2009 Julian Zappala (jxz@cs.nott.ac.uk)
 * 
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

public class DemoSimulator {

    /**
     * Time for which execution pauses so that GUI can update.
     * Reducing this value causes the simulation to run faster.
     */
	private static int DELAY = 500;
	
	/**
	 * Number of days to execute
	 */
	private static int DURATION = 10;
	
	private static volatile boolean PAUSED = false;
	
	public static void main(String[] args) {
		
		InputStream is = DemoSimulator.class.getClass().getResourceAsStream( "/META-INF/application.properties" );
		Properties pr = new Properties();
		try {
			pr.load(is);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(pr.getProperty("neco"));

		
		
		
		
		
		
        // Create an environment
        Environment env = Environment.getInstance();
        // Create our bee
        Bee b = new DemoBee();
        Bee b2 = new DemoBee();
        // Create a GUI window to show our bee
        BeeViewer bv = new BeeViewer(b);
        BeeViewer bv2 = new BeeViewer(b2);
        bv.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        // Start executing the bee
        while (!PAUSED && env.getTimestep()<(DURATION * Environment.DAY_LENGTH)) {
            // Advance the environment timestep
            env.tick();
            // Update the GUI
            bv.tick(env);
            bv2.tick(env);
            // Get the current view of the bee
            Cell[][] view = env.getView(b.getPosition(), Bee.VIEW_RANGE);
            Cell[][] view2 = env.getView(b2.getPosition(), Bee.VIEW_RANGE);
            // Let the bee choose an action
            Action a = b.senseAndAct(view, env.getTimestep());
            Action a2 = b2.senseAndAct(view2, env.getTimestep());
            // Try and execute the action
            try {
                a.execute(env, b);
                a2.execute(env, b2);
            } catch (DeadBeeException dbe) {
                System.out.println("Bee has died!");
                break;
            } catch (ActionFailedException afe) {
                System.err.println("Failed: " + afe.getMessage());
            }
            try { Thread.sleep(DELAY);} catch (Exception e) { }
        }
    }
	
	public static synchronized void pause() {
		PAUSED = !PAUSED;
	}

	
}
